- 
                Notifications
    You must be signed in to change notification settings 
- Fork 167
ReadableStream.prototype.text(), .blob(), and .bytes() #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| otherwise. | ||
| 1. Assert: either |signal| is undefined, or |signal| [=implements=] {{AbortSignal}}. | ||
| 1. Let |decode as Uint8Array| be an algorithm that takes a [=byte sequence=] |bytes| and runs the steps: | ||
| 1. Let |arrayBuffer| be a new {{ArrayBuffer}} whose contents are |bytes|. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a faithful copy of the text in fetch, but fetch has a bug which is copied here. It doesn't necessarily need to be fixed here, just gotta make sure it gets fixed here as well when fixed upstream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just random comments at the moment, not a full review.
| 1. Let |successSteps| given a [=byte sequence=] |bytes| be to run |processBody| given |bytes|. | ||
| 1. Let |errorSteps| optionally given an exception |exception| be to run |processBodyError| given |exception|. | ||
| 1. Let |reader| be the result of getting a reader for |stream|. If that threw an exception, then run |errorSteps| with that exception and return. | ||
| 1. [=read all bytes|Read all bytes=] from |reader|, given |successSteps| and |errorSteps|. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Bikeshed can compensate for different capitalisation automatically.
|  | ||
| It performs the following steps: | ||
|  | ||
| 1. Assert: |stream| [=implements=] {{ReadableStream}}. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could have a PrepareToConsume abstract op to encapsulate these common steps?
|  | ||
| dictionary ConsumeBytesOptions { | ||
| AbortSignal signal; | ||
| USVString type = ""; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This typeis not super clear about what type it means, maybeblobType? Especially if we want to put more stream related options, because we already havetypeinUnderlyingSource.
- Blob uses DOMString instead of USVString: https://w3c.github.io/FileAPI/#ref-for-dfn-BPtype
- Should we have a separate dictionary without typefor other non-blob methods?
| </div> | ||
|  | ||
| <div algorithm> | ||
| To <dfn lt="fully read">fully read</dfn> a {{ReadableStream}} |stream|, given an algorithm |processBody|, and an algorithm |processBodyError|, run these steps. |processBody| must be an algorithm accepting a [=byte sequence=]. |processBodyError| must be an algorithm optionally accepting an exception. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this lt? Same for other <dfn>s.
| </div> | ||
|  | ||
| <div algorithm> | ||
| The <dfn lt="consume fully with abort">consume fully with abort</dfn> algorithm, given a {{ReadableStream}} |stream|, an optional {{AbortSignal}} |signal|, and an algorithm that takes a [=byte sequence=] and returns a JavaScript or throws an exception |convertBytesToJSValue|, runs these steps: | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"consume fully" or "fully consume"? We already have "fully read", so maybe consistency?
And also "an algorithm |convertBytesToJSValue| that ..."
|  | ||
| 1. Let |promise| be [=a new promise=]. | ||
| 1. Let |errorSteps| given |error| to be [=reject=] |promise| with |error|. | ||
| 1. Let |successSteps| given a [=byte sequence=] |data| be to [=resolve=] |promise| with the result of running |convertBytesToJSValue| with |data|. If that threw an exception, then run |errorSteps| with that exception. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you want to name them as processBody and processBodyError as that's what the parameters of "fully read" are? Or maybe "fully read" parameters should be success/errorSteps instead.
| 1. If |stream|.[=ReadableStream/[[state]]=] is "`readable`", [=set/append=] the following action action to |actions|: | ||
| 1. return ! [$ReadableStreamCancel$](|stream|, |error|). | ||
| 1. Otherwise, return [=a promise resolved with=] undefined. | ||
| 1. [=Shutdown with an action=] consisting of [=getting a promise to wait for all=] of the actions | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pipeTo specific, maybe we can simply call ReadableStreamCancel?
Adds utility methods to akin to the Body mixin for fully reading the contents of a ReadableStream as either text, a Blob, or a Uint8Array.
dbf22e9    to
    fe482f8      
    Compare
  
    | @jasnell hey, whats the holdup with this? is there anything we from Deno can do to push this forward? If you don't have time, I do not mind taking this over. Gonna already explicitly confirm that Deno does have interest in having this. | 
| Yeah time disappeared on me. Happy to hand it over if you have the cycles. I just had to push it down the priority stack and haven't been able to get back to it | 
Adds utility methods to akin to the Body mixin for fully reading the contents of a ReadableStream as either text, a Blob, or a Uint8Array.
This is a first pass at addressing #1019 ....
In this, I add only the
text(),bytes(), andblob()methods.text()returns a promise for UTF-8 decoded text,bytes()returns a promise for aUint8Array, andblob()returns a promise for aBlob. It is modeled heavily on the fetch specBodymixin definitions with a few tweaks.Refs: #1019
(See WHATWG Working Mode: Changes for more details.)
Preview | Diff